package A_review.review9_12;

public class BinaryFind {

    public  int  find(int[] nums,int target){
       int left=0;
       int right= nums.length-1;
       while (left<=right){
           int mid=left+(right-left)/2;
           if (nums[mid]==target){
               return mid;
           }else if (nums[mid]<target){
               left=mid+1;
           }else  if (nums[mid]>target){
               right=mid-1;

           }
       }
       return  -1;
    }


    //TODO:2023/9/12  再把书看一遍
    public  int  findleft(int[] nums,int target){
        int left=0;
        int right= nums.length-1;
        while (left<=right){
            int mid=left+(right-left)/2;
            if (nums[mid]==target){
//            left=mid-1;
                right=mid-1;
            }else if (nums[mid]<target){
                left=mid+1;
            }else  if (nums[mid]>target){
                right=mid-1;

            }
        }


        //TODO:2023/9/12
        if (left>= nums.length||nums[left]!=target){
            return  -1;
        }


        return left;
    }


    public  int  findright(int[] nums,int target){

        int left=0;
        int right= nums.length-1;
        while (left<=right){
            int mid=left+(right-left)/2;
            if (nums[mid]==target){
          left=mid+1;
            }else if (nums[mid]<target){
                left=mid+1;
            }else  if (nums[mid]>target){
                right=mid-1;

            }
        }

        if (right<0||nums[right]!=target){
            return  -1;
        }




        return right;

    }



}
